#!/usr/bin/env python
# coding=utf-8

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))

import MySQLdb,time,threading
from vavava.base import LogAdapter

__author__ = 'vavava'


class _dbhelper:
    def __init__(self,conn=None,log=None):
        self.conn = conn
        if self.conn:
            self.cursor = conn.cursor()
        else:
            self.cursor = None
        self.log = log

    def _init_cursor(self,conn=None):
        if conn is None and self.conn is None:
            raise Exception("No conn available")
        if conn is None:
            conn = self.conn
        if self.cursor:
            self.close()
        self.cursor = conn.cursor()

    def _fecthall(self,sql_str):
        try:
            res = self.cursor.fetchall(sql_str)
            return res
        except Exception as e:
            self.log.exception(e)

    def _insert(self,sql):
        try:
            self.conn.begin()
            self.cursor = self.conn.cursor()
            self.cursor.execute(sql)
            self.conn.commit()
        except Exception as e:
            self.log.exception(e)

    def __del__(self):
        if self.cursor:
            self.cursor.close()

